<template>
  <table-box ref="tableBox" class="table-height-auto" :data="tableData" :table-config="tableConfig" />
</template>
<script>
export default {
  name: 'ShopTable',
  props: {
    tableData: {
      type: Array,
      default: () => []
    },
    cooperationEnd: {
      type: Object,
      default: () => ({})
    },
    isEdit: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      tableConfig: {
        hideFuncBar: true,
        setting: [
          {
            prop: 'code',
            label: '门店编码',
            minWidth: 100
          },
          {
            prop: 'name',
            label: '门店名称',
            minWidth: 100
          },
          {
            prop: 'shopAddr',
            label: '详细地址',
            minWidth: 100
          },
          {
            prop: 'operateArea',
            label: '经营面积(㎡)',
            minWidth: 100
          },
          {
            prop: 'businessTypeName',
            label: '现经营业务类型',
            minWidth: 120
          },
          {
            prop: 'isNewBusinessTakeover',
            label: '新商是否接管该门店',
            minWidth: 200,
            render: (h, row) => {
              if (!this.isEdit) {
                return h('c-dict-range-view', {
                  props: {
                    appCode: 'slm',
                    domainCode: 'marketing',
                    dictTypeCode: 'BOOLEANS',
                    value: row.isNewBusinessTakeover
                  }
                })
              }
              return h('c-dict-select', {
                props: {
                  value: row.isNewBusinessTakeover,
                  dictTypeCode: 'BOOLEANS',
                  appCode: 'slm',
                  domainCode: 'marketing',
                  clearable: true,
                  placeholder: '请选择'
                },
                attrs: {
                  clearable: true
                },
                on: {
                  input: v => this.$set(row, 'isNewBusinessTakeover', v)
                },
                nativeOn: {
                  click: e => e.stopPropagation()
                }
              })
            }
          }
        ]
      }
    }
  },
  watch: {
    cooperationEnd: {
      handler(v) {
        const t = this.tableConfig.setting.find(e => e.prop === 'isNewBusinessTakeover')
        if (t) {
          this.$set(t, 'required', v.type === 'C' && this.isEdit)
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    validate() {
      return new Promise((resolve, reject) => {
        const requireds = this.tableConfig.setting.filter(e => e.required)
        if (requireds.length <= 0 || !this.isEdit) return resolve('')
        const msgs = []
        for (let i = 0; i < this.tableData.length; i++) {
          const t = this.tableData[i]
          for (const r of requireds) {
            if (t[r.prop] === '' || t[r.prop] === undefined || t[r.prop] === null) {
              msgs.push(`【经销商-门店信息】第${i + 1}行【${r.label}】未选择或填写`)
            }
          }
        }
        if (msgs.length > 0) {
          this.$message({
            type: 'warning',
            message: msgs.join('<br>'),
            dangerouslyUseHTMLString: true
          })
          reject(false)
        }
        resolve(true)
      })
    }
  }
}
</script>
